﻿using System;
using System.Collections.Generic;

namespace EasySL.Core
{
    public class Task : TaskBase
    {
        public Type ReturnType { get; set; }
        public string MethodName { get; set; }

        public GetDataAsyncCompleted Callback;

        public Task()
            : base()
        {
            this.requestor = new Requestor();
            this.request = new Request();
        }

        protected override void RunProc(object state)
        {
            this.request.MethodName = MethodName;
            this.requestor.GetDataAsync(this.request, this.ReturnType, state, this.GetDataCallback);
        }

        protected void GetDataCallback(Response response)
        {
            if (this.Callback != null)
                this.Callback(response);

            if (response.Status == ResponseStatus.Success)
                this.RunFinished();
        }

        public void SetParameter(string key, object value)
        {
            if (!this.request.Parameters.ContainsKey(key))
                this.request.Parameters.Add(key, value);
            else
                this.request.Parameters[key] = value;
        }

        private Requestor requestor;
        private Request request;
    }
}
